package com.h2.gwt.util.load;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SyncLoader {
   
   private List<Runnable> _tasks = new ArrayList<Runnable>();
   
   private Iterator<Runnable> _iterator;
   
   private Integer _index = 0;
   
   ////
   ////
   
   public void start() {
      setIterator(getTasks().iterator());
      next();
   }
   
   public void onFinish() {
      //overloaded method
   }
   
   public void next() {
      if(getIterator().hasNext()) {
         setIndex(getIndex() + 1);
         getIterator().next().run();
      }
      else {
         onFinish();
      }
   }
   
   public void add(Runnable task) {
      getTasks().add(task);
   }

   /**
    * @return the tasks
    */
   public List<Runnable> getTasks() {
      return _tasks;
   }

   /**
    * @param tasks the tasks to set
    */
   public void setTasks(List<Runnable> tasks) {
      _tasks = tasks;
   }

   /**
    * @return the iterator
    */
   public Iterator<Runnable> getIterator() {
      return _iterator;
   }

   /**
    * @param iterator the iterator to set
    */
   public void setIterator(Iterator<Runnable> iterator) {
      _iterator = iterator;
   }

   /**
    * @return the index
    */
   public Integer getIndex() {
      return _index;
   }

   /**
    * @param index the index to set
    */
   public void setIndex(Integer index) {
      _index = index;
   }
}
